﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class newslist : System.Web.UI.Page
{
    BLL.NewsBll nb = new BLL.NewsBll();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCategories();

            if (Request.QueryString["category"] == null)
            {
                BindAllNews();
                Paging();
            }
            else
            {
                BindNewsByCategory();
                CategoryPaging();
            }

            //Response.Write(SiteMap.CurrentNode.Title.ToString());
            
        }
    }

    private void Paging()
    {
        int pageNow = 1;
        if (Request.QueryString["page"] != null)
        {
            pageNow = int.Parse(Request.QueryString["page"]);
        }
        int NewsCount = nb.GetNewsCount();
        int pageCount = 1;
        if ((NewsCount % 20) == 0)
        {
            pageCount = NewsCount / 20;
        }
        else
        {
            pageCount = NewsCount / 20 + 1;
        }

        lb_SumPage.Text = "共" + NewsCount + "条 " + pageNow + "/"+pageCount+"页 每页20条";

        //if (pageNow == 1)
        //{
        //    prev.Style["display"] = "none";
        //}
        //else
        //{
        //    prev.Style["display"] = "block";
        //}

        //if (pageNow == pageCount)
        //{
        //    next.Style["display"] = "none";
        //}
        //else
        //{
        //    next.Style["display"] = "block";
        //}

        if (pageNow != 1)
        {
            hl_BeginPage.NavigateUrl = "newslist.aspx?page=1";
            hl_PrevPage.NavigateUrl = "newslist.aspx?page=" + (pageNow - 1);
        }
        if (pageCount != pageNow)
        {
            hl_EndPage.NavigateUrl = "newslist.aspx?page=" + pageCount;
            hl_NextPage.NavigateUrl = "newslist.aspx?page=" + (pageNow + 1);
        }
    }

    private void CategoryPaging()
    {
        int pageNow = 1;
        if (Request.QueryString["page"] != null)
        {
            pageNow = int.Parse(Request.QueryString["page"]);
        }
        int category = int.Parse(Request.QueryString["category"]);
        int NewsCount = nb.GetNewsCountByCategory(category);
        int pageCount = 1;
        if ((NewsCount % 20) == 0)
        {
            pageCount = NewsCount / 20;
        }
        else
        {
            pageCount = NewsCount / 20 + 1;
        }

        lb_SumPage.Text = "共" + NewsCount + "条 " + pageNow + "/" + pageCount + "页 每页20条";

        //if (pageNow == 1)
        //{
        //    prev.Style["display"] = "none";
        //}
        //else
        //{
        //    prev.Style["display"] = "block";
        //}

        //if (pageNow == pageCount)
        //{
        //    next.Style["display"] = "none";
        //}
        //else
        //{
        //    next.Style["display"] = "block";
        //}
        if (pageNow != 1)
        {
            hl_BeginPage.NavigateUrl = "newslist.aspx?category=" + category + "&page=1";
            hl_PrevPage.NavigateUrl = "newslist.aspx?category=" + category + "&page=" + (pageNow - 1);
        }
        if (pageNow != pageCount)
        {
            hl_EndPage.NavigateUrl = "newslist.aspx?category=" + category + "&page=" + pageCount;
            hl_EndPage.NavigateUrl = "newslist.aspx?category=" + category + "&page=" + (pageNow + 1);
        }
    }


    private void BindAllNews()
    {
        int pageNow = 1;
        if (Request.QueryString["page"] != null)
        {
            pageNow = int.Parse(Request.QueryString["page"]);
        }
        DataTable table = nb.GetAllNews(pageNow);
        rep_newsContent.DataSource = table;
        rep_newsContent.DataBind();
    }

    private void BindNewsByCategory()
    {
        int pageNow = 1;
        if (Request.QueryString["page"] != null)
        {
            pageNow = int.Parse(Request.QueryString["page"]);
        }
        int category = int.Parse(Request.QueryString["category"].ToString());
        DataTable table = nb.GetNewsByCategory(category,pageNow);
        rep_newsContent.DataSource = table;
        rep_newsContent.DataBind();
    }

    private void BindCategories()
    {
        DataTable table = nb.GetNewsCategory();
        rep_NewsCategory.DataSource = table;
        rep_NewsCategory.DataBind();
    }

}
